package com.neusoft.my12306.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.my12306.common.res.CommonResult;
import com.neusoft.my12306.entity.User;
import com.neusoft.my12306.exception.CodeSenderException;
import com.neusoft.my12306.mapper.UserMapper;
import com.neusoft.my12306.service.IUserService;
import com.neusoft.my12306.utils.GenerateRandomCodeUtil;
import com.neusoft.my12306.utils.SendPhoneCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 葛彬
 * @since 2023-07-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    public static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public CommonResult register(User user) {
        user.setUserid(null);
        int i = baseMapper.insert(user);
        CommonResult result = new CommonResult(user.getUserid());
        if (i <= 0) {
            result.setNotModified();
        }
        return result;
    }

    @Override
    public CommonResult sendPhoneMessage(User user) {
        GenerateRandomCodeUtil randomCodeUtil = new GenerateRandomCodeUtil();
        String code = randomCodeUtil.createCode();
        SendPhoneCodeUtil sendPhoneCodeUtil = new SendPhoneCodeUtil();
        try {
            sendPhoneCodeUtil.sendCode(user.getMobile(), code);
            return new CommonResult(code);
        } catch (CodeSenderException codeSenderException) {
            LOGGER.error("短信发送失败", codeSenderException);
            return new CommonResult(500, codeSenderException.getMessage(), code);
        }
    }

    @Override
    public CommonResult login(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        User u = baseMapper.selectOne(queryWrapper);
        CommonResult result = new CommonResult(u);

        if (u == null)
            result.setNotFound();
        return result;
    }

    @Override
    public CommonResult queryPassenger(List<Integer> passids) {

        List<User> users = new ArrayList<>(); 
        for(int i = 0;i<passids.size();i++)
        {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("userid", passids.get(i));
            User u = baseMapper.selectOne(queryWrapper);
            users.add(u);
        }
        CommonResult result = new CommonResult(users);

        if (users == null)
            result.setNotFound();
        return result;
    }

    @Override
    public CommonResult pay(String name, Double money) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        User u = baseMapper.selectOne(queryWrapper);
        u.setAmount(u.getAmount()-money);
        int i =  baseMapper.updateById(u);

        CommonResult result = new CommonResult(i);
        if (i <= 0 ){
            result.setNotModified();
        }
        return result;
    }

    @Override
    public CommonResult queryMyInfo(User user) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", user.getName());
        User u = baseMapper.selectOne(queryWrapper);
        CommonResult result = new CommonResult(u);
        if (u == null)
            result.setNotFound();
        return result;
    }
}
